package com.fcl.oschina.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.fcl.mylibrary.recyclerviewhelper.BaseAdapter;
import com.fcl.mylibrary.util.FLog;
import com.fcl.oschina.R;
import com.fcl.oschina.adapter.TweetAdapter;
import com.fcl.oschina.entity.TweetEntity;
import com.fcl.oschina.model.TweetModel;
import com.fcl.oschina.ui.activity.TweetDetailActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class TweetFragment extends BaseRecyclerFragment<TweetEntity> implements TweetView{
    private String TAG = "TweetFragment";
    private TweetModel mTweetModel;
    private TweetAdapter mTweetAdapter;
    private int mTweetType=2;

    @Override
    protected void initData() {
        super.initData();
        mTweetModel = new TweetModel(this);
        mTweetModel.setNextPage("");
        mTweetModel.getTweetList(mTweetType);
        mTweetAdapter = (TweetAdapter) mAdapter;
        mTweetAdapter.setItemClickListener(new BaseAdapter.MyClickListener() {
            @Override
            public void onClick(int pos) {
                Intent intent = new Intent(getActivity(), TweetDetailActivity.class);
                intent.putExtra("id", mDatas.get(pos).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new TweetAdapter(getContext(), R.layout.item_tweet, mDatas);
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        mSceneLayout.setLoading();
        mTweetModel.setNextPage("");
        mTweetModel.getTweetList(mTweetType);
    }

    @Override
    public void setData(final List<TweetEntity> data, final boolean add) {
        FLog.error(TAG, " thread" + Thread.currentThread().getId());
        if (add) {
            mDatas.addAll(data);
        } else {
            mDatas=data;

        }
//                mStickyLayout.showFooter(false);
        mTweetAdapter.setDatas(mDatas);
        mStickyLayout.finishRefresh();
        mSceneLayout.setNormal();
    }

    @Override
    public void noMoreData() {
        mTweetAdapter.noMoreData();
    }

    @Override
    protected void loadMore(int curPage) {
        super.loadMore(curPage);
        mTweetModel.getTweetList(mTweetType);
    }
}
